﻿using KinonekoSoftware.ArkSpark.Services.Highlighting;
using KinonekoSoftware.CodariaDB.Documents.Index;
using KinonekoSoftware.CodariaDB.Runtimes;
using KinonekoSoftware.Extensions.Ioc;
using KinonekoSoftware.Extensions.Strings.Core;
using KinonekoSoftware.Extensions.Strings.SearchModels.TrieTree;
using KinonekoSoftware.Foundation.UI;
using KinonekoSoftware.Foundation.UI.Core;
using KinonekoSoftware.Foundation.UI.ViewModels;
using KinonekoSoftware.Platform.Controls;
using KinonekoSoftware.Platform.Windows;
using KinonekoSoftware.Studio.Services;

namespace KinonekoSoftware.Studio
{
    public class JuXiaoYouWindow : TabbedWindow
    {
        protected override async Task<bool> CanClose()
        {
            var target  = ViewModel<AppViewModel>();
            var runtime = Ioc.Get<RepositoryRuntime>();
            
            //
            //
            if (runtime.Mode == DataMode.Protected)
            {
                var r = await Ioc.Get<IDialogService>().Question(TextSR.Close_Protected, TextSR.Close_Protected_Dump, TextSR.Close_Protected_Exit);
                if (r == TripleSelection.Cancel)
                {
                    return false;
                }
                
                if (r == TripleSelection.Option1)
                {
                    //
                    // 退出时保存数据。
                    await runtime.CloseAsync(true);
                }

                Ioc.Get<ShellCore>().Stop();
                return true;
            }

            if (target.ShouldDirectExit)
            {
                Ioc.Get<ShellCore>().Stop();
                return true;
            }
            
            if (!await ViewModelBase.DangerOperation(
                TitleSR.Close_Window,
                TextSR.Close_UnSave))
            {
                return false;
            }

            Ioc.Get<ShellCore>().Stop();
            return true;
        }
        
        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.F12 && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
            {
                ViewModel<AppViewModel>().Snapshot(x => Shell.CaptureAsync(x as FrameworkElement, 120));
                e.Handled = true;
            }
            
            base.OnPreviewKeyUp(e);
        }
    }
}